//
//  ListViewController.m
//  Shanghai Cuisine
//
//  Created by iOS Two on 14/12/15.
//  Copyright (c) 2014年 iOS Two. All rights reserved.
//

#import "ListViewController.h"
#import "UIImageView+MJWebCache.h"
#import "DetailViewController.h"

@interface ListViewController ()

@end

@implementation ListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 30)];
    titleLabel.text = @"精品菜系";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    titleLabel.textColor = [UIColor colorWithRed:0.97 green:0.69 blue:0.17 alpha:1];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.97 green:0.69 blue:0.17 alpha:1];
    self.navigationItem.titleView = titleLabel;
    [self creatController];
    [self getData];
    // Do any additional setup after loading the view.
}
-(void)creatController {
    _mainTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kwidth, kHeight - 64)];
    _mainTable.dataSource = self;
    _mainTable.delegate = self;
    _mainTable.rowHeight = 122;
    _mainTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_mainTable];
}
-(void)getData {
    [self showIndeterminateProgress];
    _nameArray = [[NSMutableArray alloc]init];
    _imgArray = [[NSMutableArray alloc]init];
    _bigImg = [[NSMutableArray alloc]init];
    _materialArray = [[NSMutableArray alloc]init];
    _materialQuantity = [[NSMutableArray alloc]init];
    _dishId = [[NSMutableArray alloc]init];
    _tips = [[NSMutableArray alloc]init];
    _auxiliaryMaterial = [[NSMutableArray alloc]init];
    _auxiliaryQuantity = [[NSMutableArray alloc]init];
    _introductionArray = [[NSMutableArray alloc]init];
    AVQuery *query = [[AVQuery alloc]initWithClassName:@"dishName"];
    //    query.cachePolicy = kPFCachePolicyNetworkElseCache;
    //    query.maxCacheAge = 24 * 3600 * 30;//缓存时间有效期
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        for (int i = 0; i < objects.count; i++) {
            [_nameArray addObject:[[objects objectAtIndex:i] objectForKey:@"dishName"]];
            [_imgArray addObject:[[objects objectAtIndex:i] objectForKey:@"imgUrl"]];
            [_bigImg addObject:[[objects objectAtIndex:i] objectForKey:@"bigImg"]];
            [_materialArray addObject:[[objects objectAtIndex:i] objectForKey:@"material"]];
            [_materialQuantity addObject:[[objects objectAtIndex:i] objectForKey:@"materialQuantity"]];
            [_dishId addObject:[[objects objectAtIndex:i] objectForKey:@"objectId"]];
            [_tips addObject:[[objects objectAtIndex:i] objectForKey:@"tips"]];
            [_auxiliaryQuantity addObject:[[objects objectAtIndex:i] objectForKey:@"auxiliaryQuantity"]];
            [_auxiliaryMaterial addObject:[[objects objectAtIndex:i] objectForKey:@"auxiliaryMaterial"]];
            [_introductionArray addObject:[[objects objectAtIndex:i] objectForKey:@"introduction"]];
        }
        if (_dishId.count % 3 == 1) {
            [_nameArray addObject:@"0"];
            [_imgArray addObject:@"0"];
            [_bigImg addObject:@"0"];
            [_materialArray addObject:@"0"];
            [_materialQuantity addObject:@"0"];
            [_dishId addObject:@"0"];
            [_tips addObject:@"0"];
            [_auxiliaryQuantity addObject:@"0"];
            [_auxiliaryMaterial addObject:@"0"];
            [_introductionArray addObject:@"0"];
            [_nameArray addObject:@"0"];
            [_imgArray addObject:@"0"];
            [_bigImg addObject:@"0"];
            [_materialArray addObject:@"0"];
            [_materialQuantity addObject:@"0"];
            [_dishId addObject:@"0"];
            [_tips addObject:@"0"];
            [_auxiliaryQuantity addObject:@"0"];
            [_auxiliaryMaterial addObject:@"0"];
            [_introductionArray addObject:@"0"];
        } else if (_dishId.count % 3 == 2) {
            [_nameArray addObject:@"0"];
            [_imgArray addObject:@"0"];
            [_bigImg addObject:@"0"];
            [_materialArray addObject:@"0"];
            [_materialQuantity addObject:@"0"];
            [_dishId addObject:@"0"];
            [_tips addObject:@"0"];
            [_auxiliaryQuantity addObject:@"0"];
            [_auxiliaryMaterial addObject:@"0"];
            [_introductionArray addObject:@"0"];
        }
        [HUD hide:YES];
        [_mainTable reloadData];
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _introductionArray.count / 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(cell == nil){
        cell = [[MainTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    } else {
        for (UIView *view in [cell.contentView subviews]) {
            [view removeFromSuperview];
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
    
    if ([[_dishId objectAtIndex:indexPath.row * 3 + 1] isEqualToString:@"0"]) {
        [cell.contentView addSubview:cell.back1];
        cell.name1.text = [_nameArray objectAtIndex:(indexPath.row ) * 3];
        [cell.contentView addSubview:cell.name1];
        
        [cell.image1 setImageURLStr:[_imgArray objectAtIndex:(indexPath.row ) * 3] placeholder:[UIImage imageNamed:@"img_delete_advertisement"]];
        cell.image1.tag = (indexPath.row ) * 3;
        [cell.image1 addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buttonClick:)]];
        [cell.contentView addSubview:cell.image1];
    } else if ([[_dishId objectAtIndex:indexPath.row * 3 + 2] isEqualToString:@"0"]) {
        [cell.contentView addSubview:cell.back1];
        [cell.contentView addSubview:cell.back2];

        cell.name1.text = [_nameArray objectAtIndex:(indexPath.row ) * 3];
        cell.name2.text = [_nameArray objectAtIndex:(indexPath.row ) * 3 + 1];
        [cell.contentView addSubview:cell.name1];
        [cell.contentView addSubview:cell.name2];
        
        [cell.image1 setImageURLStr:[_imgArray objectAtIndex:(indexPath.row ) * 3] placeholder:[UIImage imageNamed:@"img_delete_advertisement"]];
        cell.image1.tag = (indexPath.row ) * 3;
        [cell.image1 addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buttonClick:)]];
        
        [cell.image2 setImageURLStr:[_imgArray objectAtIndex:(indexPath.row ) * 3 + 1] placeholder:[UIImage imageNamed:@"img_delete_advertisement"]];
        cell.image2.tag = (indexPath.row ) * 3 + 1;
        [cell.image2 addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buttonClick:)]];
        [cell.contentView addSubview:cell.image1];
        [cell.contentView addSubview:cell.image2];
    } else {
        [cell.contentView addSubview:cell.back1];
        [cell.contentView addSubview:cell.back2];
        [cell.contentView addSubview:cell.back3];

        cell.name1.text = [_nameArray objectAtIndex:(indexPath.row ) * 3];
        cell.name2.text = [_nameArray objectAtIndex:(indexPath.row ) * 3 + 1];
        cell.name3.text = [_nameArray objectAtIndex:(indexPath.row ) * 3 + 2];
        [cell.contentView addSubview:cell.name1];
        [cell.contentView addSubview:cell.name2];
        [cell.contentView addSubview:cell.name3];
        
        [cell.image1 setImageURLStr:[_imgArray objectAtIndex:(indexPath.row ) * 3] placeholder:[UIImage imageNamed:@"img_delete_advertisement"]];
        cell.image1.tag = (indexPath.row ) * 3;
        [cell.image1 addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buttonClick:)]];
        
        [cell.image2 setImageURLStr:[_imgArray objectAtIndex:(indexPath.row ) * 3 + 1] placeholder:[UIImage imageNamed:@"img_delete_advertisement"]];
        cell.image2.tag = (indexPath.row ) * 3 + 1;
        [cell.image2 addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buttonClick:)]];
        
        [cell.image3 setImageURLStr:[_imgArray objectAtIndex:(indexPath.row ) * 3 + 2] placeholder:[UIImage imageNamed:@"img_delete_advertisement"]];
        cell.image3.tag = (indexPath.row ) * 3 + 2;
        [cell.image3 addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buttonClick:)]];
        [cell.contentView addSubview:cell.image1];
        [cell.contentView addSubview:cell.image2];
        [cell.contentView addSubview:cell.image3];
    }
    return cell;
}
-(void)buttonClick:(UITapGestureRecognizer *)sender {
    [MobClick event:@"clickImage"];
    DetailViewController *detail = [[DetailViewController alloc]init];
    detail.name = [_nameArray objectAtIndex:sender.view.tag];
    detail.bigImg = [_bigImg objectAtIndex:sender.view.tag];
    detail.dishId = [_dishId objectAtIndex:sender.view.tag];
    detail.materialArray = [_materialArray objectAtIndex:sender.view.tag];
    detail.materialQuantity = [_materialQuantity objectAtIndex:sender.view.tag];
    detail.tips = [_tips objectAtIndex:sender.view.tag];
    detail.auxiliaryMaterial = [_auxiliaryMaterial objectAtIndex:sender.view.tag];
    detail.auxiliaryQuantity = [_auxiliaryQuantity objectAtIndex:sender.view.tag];
    detail.introduction = [_introductionArray objectAtIndex:sender.view.tag];
    detail.smallImg = [_imgArray objectAtIndex:sender.view.tag];
    self.navigationItem.title = @"返回";

    [self.navigationController pushViewController:detail animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
